package com.easylinkin.linkappapi.trainingpenetrate.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.common.utils.StringUtil;
import com.easylinkin.linkappapi.trainingpenetrate.dto.RailLinkappSafetyEduPlanDTO;
import com.easylinkin.linkappapi.trainingpenetrate.service.IRailLinkappSafetyEduPlanService;
import com.easylinkin.linkappapi.trainingpenetrate.vo.RailLinkappSafetyEduPlanVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import java.util.Objects;

/**培训穿透，培训信息相关接口
 * @author lyk
 * @date 2025/7/1 下午 4:57
 */
@RestController
@RequestMapping("/safetyEdu")
public class RailLinkappSafetyEduController {

    @Autowired
    private IRailLinkappSafetyEduPlanService railLinkappSafetyEduPlanService;


    /**
     * 列表分页查询
     * @param requestModel
     * @return
     */
    @PostMapping(value = "/getPage")
    public RestMessage selectPage(@RequestBody RequestModel<RailLinkappSafetyEduPlanDTO> requestModel) {
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<RailLinkappSafetyEduPlanVO> record = railLinkappSafetyEduPlanService.selectPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder(record).build();
    }

    /**
     *  培训信息保存接口
     * @param dto
     * @return
     */
    @PostMapping(value = "/save")
    public RestMessage save(@RequestBody RailLinkappSafetyEduPlanDTO dto){
        //新增校验
        if (StringUtils.isEmpty(dto.getTrainingType())){
            return RestBuilders.failureMessage().setMessage("培训类型不能为空");
        }
        if (StringUtils.isEmpty(dto.getName())){
            return RestBuilders.failureMessage().setMessage("培训名称不能为空");
        }
        if (StringUtils.isEmpty(dto.getEduDate())){
            return RestBuilders.failureMessage().setMessage("培训时间不能为空");
        }
        if (StringUtils.isEmpty(dto.getContent())){
            return RestBuilders.failureMessage().setMessage("培训内容不能为空");
        }
        if (StringUtils.isEmpty(dto.getCuratorId())){
            return RestBuilders.failureMessage().setMessage("培训负责人不能为空");
        }
        if (Objects.isNull(dto.getId())){
            //新增
            if (Objects.isNull(dto.getAddList())){
                return RestBuilders.failureMessage().setMessage("培训人员不能为空");
            }
            if (dto.getAddList().size() == 0){
                return RestBuilders.failureMessage().setMessage("请选中参与培训的人员");
            }
            railLinkappSafetyEduPlanService.add(dto);
        }else{
            //编辑
            railLinkappSafetyEduPlanService.edit(dto);
        }
        return RestBuilders.successMessage().setMessage("操作成功");
    }

}
